﻿//-----------------------------------------------------------------------
// <copyright file="FacetCategory.cs" company="Forebrain Technology, LLC">
//
// Copyright (c) Forebrain Technology, LLC. All rights reserved.
// 
// This source code is released under the Microsoft Public License (Ms-PL).
//
// </copyright>
//-----------------------------------------------------------------------
namespace PivotCollectionTools.Xml
{
    using System;
    using System.Xml.Serialization;
    using System.Globalization;

    /// <summary>
    /// A grouping of facets. Note that all values (see children of Facet below) for a 
    /// FacetCategory must be of the same type.
    /// </summary>
    /// <remarks>
    /// For each facet attached to an item, a corresponding facet category must exist. Here, the name 
    /// of the facet, the type, a format string, and various visibility options can be set. The Type 
    /// field can be Number, String, LongString, DateTime, or Link.
    /// </remarks>
    [Serializable()]
    [XmlType(Namespace = "http://schemas.microsoft.com/collection/metadata/2009")]
    public class FacetCategory : XmlNodeBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FacetCategory"/> class.
        /// </summary>
        public FacetCategory()
            : base()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FacetCategory"/> class.
        /// </summary>
        /// <param name="name">The name of the FacetCategory.</param>
        /// <param name="type">The type of the FacetCategory.</param>
        /// <param name="isFilterVisible">if set to <c>true</c> category appears in the filter panel.</param>
        /// <param name="isWordWheelVisible">if set to <c>true</c> category is included in keyword filters.</param>
        /// <param name="isMetadataVisible">if set to <c>true</c> category is visible in the info panel.</param>
        public FacetCategory(string name, FacetDataType type, bool? isFilterVisible, bool? isWordWheelVisible, bool? isMetadataVisible)
            : base()
        {
            this.Name = name;
            this.FacetType = type;

            if (isFilterVisible != null)
            {
                this.IsFilterVisibleValue = isFilterVisible.Value ? "true" : "false";
            }

            if (isWordWheelVisible != null)
            {
                this.IsWordWheelVisibleValue = isWordWheelVisible.Value ? "true" : "false";
            }

            if (isMetadataVisible != null)
            {
                this.IsMetadataVisibleValue = isMetadataVisible.Value ? "true" : "false";
            }
        }

        /// <summary>
        /// Gets or sets a strig value indicating whether this instance is filter visible.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is filter visible; otherwise, <c>false</c>.
        /// </value>
        [XmlAttribute(AttributeName = "IsFilterVisible", Namespace = "http://schemas.microsoft.com/livelabs/pivot/collection/2009")]
        public string IsFilterVisibleValue { get; set; }

        /// <summary>
        /// Gets the is filter visible.
        /// </summary>
        /// <value>The is filter visible of the facet category.</value>
        [XmlIgnore]
        public bool? IsFilterVisible
        {
            get
            {
                if (string.IsNullOrEmpty(this.IsFilterVisibleValue))
                {
                    return null;
                }

                return this.IsFilterVisibleValue.ToUpper(CultureInfo.InvariantCulture) == "TRUE" ? true : false;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is word wheel visible.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is word wheel visible; otherwise, <c>false</c>.
        /// </value>
        [XmlAttribute(AttributeName = "IsWordWheelVisible", Namespace = "http://schemas.microsoft.com/livelabs/pivot/collection/2009")]
        public string IsWordWheelVisibleValue { get; set; }

        /// <summary>
        /// Gets the is word wheel visible.
        /// </summary>
        /// <value>The is word wheel visible of the facet category.</value>
        [XmlIgnore()]
        public bool? IsWordWheelVisible 
        {
            get
            {
                if (string.IsNullOrEmpty(this.IsWordWheelVisibleValue))
                {
                    return null;
                }

                return this.IsWordWheelVisibleValue.ToUpper(CultureInfo.InvariantCulture) == "TURE" ? true : false;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is word wheel visible.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is word wheel visible; otherwise, <c>false</c>.
        /// </value>
        [XmlAttribute(AttributeName = "IsMetadataVisible", Namespace = "http://schemas.microsoft.com/livelabs/pivot/collection/2009")]
        public string IsMetadataVisibleValue { get; set; }

        /// <summary>
        /// Gets the is metadata visible.
        /// </summary>
        /// <value>The is metadata visible of the facet category.</value>
        [XmlIgnore()]
        public bool? IsMetadataVisible
        {
            get
            {
                if (string.IsNullOrEmpty(this.IsMetadataVisibleValue))
                {
                    return null;
                }

                return this.IsMetadataVisibleValue.ToUpper(CultureInfo.InvariantCulture) == "TURE" ? true : false;
            }
        }

        /// <summary>
        /// Gets or sets the name of the FacetCategory.
        /// </summary>
        /// <value>The name of the FacetCategory.</value>
        [XmlAttributeAttribute()]
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the format.
        /// </summary>
        /// <value>The format.</value>
        [XmlAttribute()]
        public string Format { get; set; }

        /// <summary>
        /// Gets or sets the FacetCategory type.
        /// </summary>
        /// <value>The FacetCategory type.</value>
        [XmlAttribute(AttributeName = "Type")]
        public FacetDataType FacetType { get; set; }

        public override string ToString()
        {
            return this.Name;
        }
    }
}
